class Solution:
    def fractionToDecimal(self, numerator: int, denominator: int) -> str:
        negative = ""
        if numerator * denominator < 0:
            negative = "-"
        numerator = abs(numerator)
        denominator = abs(denominator)
        intPart = abs(numerator) // abs(denominator)
        rest = numerator % denominator
        intPart = negative + str(intPart)
        if rest == 0:
            return str(intPart)
        mantissa = []
        mantissaRest = []
        while rest:
            if rest in mantissaRest:
                ind = mantissaRest.index(rest)
                return str(intPart) + "." + "".join(mantissa[0:ind]) + "(" + "".join(mantissa[ind:]) + ")"
            else:
                mantissaRest.append(rest)
            rest = rest * 10
            i = str(rest // denominator)
            rest = rest % denominator
            mantissa.append(i)
        return str(intPart) + "." + "".join(mantissa)


if __name__ == '__main__':
    so = Solution()
    r = so.fractionToDecimal(50, -8)
    print(50 / -8)
    print(r)
